#include "ItemDelegate.h"
#include <QApplication>
#include <ItemReference.h>
#include <QComboBox>
#include <QLineEdit>



ItemDelegate::ItemDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{
}



QWidget* ItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (!index.isValid()) return QStyledItemDelegate::createEditor(parent, option, index);

    QObject* obj = static_cast<QObject*>(index.internalPointer());
    AbstractItem* item = dynamic_cast<AbstractItem*>(obj);
    if (!item) return QStyledItemDelegate::createEditor(parent, option, index);

    switch (item->category())
    {
    case ItemCategory_Group:
        {
            return new QLineEdit(parent);
        }
    case ItemCategory_Item:
        {
            return new QLineEdit(parent);
        }
    case ItemCategory_Reference:
        {
            if (index.column() != Item::ValueData &&
                index.column() != ItemReference::TypeData) return 0;

            return new QComboBox(parent);
        }
    }


    return QStyledItemDelegate::createEditor(parent, option, index);

}



void ItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if (!index.isValid()) return;

    QObject* obj = static_cast<QObject*>(index.internalPointer());
    AbstractItem* item = dynamic_cast<AbstractItem*>(obj);

    switch (item->category())
    {
    case ItemCategory_Group:
    case ItemCategory_Item:
        {
            QLineEdit* le = qobject_cast<QLineEdit*>(editor);
            if (!le) return;

            le->setText(index.data().toString());
            le->selectAll();
            return;
        }
    case ItemCategory_Reference:
        break;
    default:
        return;
    }


    QComboBox* combo = dynamic_cast<QComboBox*>(editor);
    if (!combo) return;

    switch (index.column())
    {
    case Item::ValueData:
        break;

    case ItemReference::TypeData:
        {
            for (int i = ItemReference::ReferenceType_FIRST; i <= ItemReference::ReferenceType_LAST; ++i)
            {
                combo->addItem(ItemReference::typeName(i), i);
            }
            return;
        }

    default:
        return;
    }

    QList<Item*> options = item->rootItem()->findChildren<Item*>();
    foreach (Item* option, options)
    {
        if (!option || option->category() != ItemCategory_Item) continue;

        combo->addItem(option->name(), option->objectName());
        //combo->addItem(QString("%1 (%2)").arg(option->name()).arg(option->objectName()), option->objectName());
    }


}



void ItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if (!index.isValid()) return;

    QComboBox* combo = dynamic_cast<QComboBox*>(editor);
    if (!combo)
    {
        QStyledItemDelegate::setModelData(editor, model, index);
        return;
    }

    model->setData(index, combo->itemData(combo->currentIndex()));

}



void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (!index.isValid()) return QStyledItemDelegate::paint(painter, option, index);

    QObject* obj = static_cast<QObject*>(index.internalPointer());
    ItemReference* item = dynamic_cast<ItemReference*>(obj);

    if (!item) return QStyledItemDelegate::paint(painter, option, index);

    QStyleOptionViewItemV4 v4option = option;
    //v4option.features = QStyleOptionViewItemV4::HasDecoration | QStyleOptionViewItemV4::HasDisplay;

    initStyleOption(&v4option, index);

    switch (index.column())
    {
    case ItemReference::ValueData:
        if (item->isValid()) v4option.text = item->referenceItem()->name();
        else                 v4option.text = "";
        break;

    case ItemReference::TypeData:
        v4option.text = ItemReference::typeName(item->data(ItemReference::TypeData).toInt());
        break;

    default:
        v4option.text = index.data().toString();
        break;
        //return QStyledItemDelegate::paint(painter, option, index);
    }

    QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &v4option, painter);
}




